<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <input type="text" id="inp">
</body>
<script>
    var ele = document.getElementById('inp')
    ele.addEventListener('keyup',debounce(function(){
        console.log('请求接口')
    }))
    function debounce(fn){
        let timer = null
        if(timer){clearTimeout(timer)}
        return function () {
            timer = setTimeout(()=>{
                fn.call(this,arguments)
                timer = null
            },1000)
        }
    }


    var oDiv = document.getElementById('box')
    ele.addEventListener('drag',function(e){
        console.log(e.clientX)
    })
    ele.addEventListener('drag',throttle((e)=>{
        console.log(e.clientX)
    }))    
    function throttle (fn) {
        let timer = null
        return function () {  //拖拽时，执行这个方法；第一次给timer赋值；第二次timer有值返回；1秒后timer为null 再次执行fn
            if(timer){return}
            timer = setTimeout(()=>{
                fn.apply(this,arguments)
                timer = null
            },1000)
        }
    }
</script>
</html>